﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ShadowCommando.Game;
using ShadowCommando.Game.Utilites;
using ShadowCommando.GameMenus;
using ShadowCommando.Utilites;
namespace ShadowCommando
{
    
    public partial class MainWindow : Form
    {
        private Game.Game game;
        private GameMenu menu;
        private bool escapePress = true;
        private string playerName = "";
        private string scoreListName = "scores";
        private string pathScoreList;
        private int gameWidth = 1200;
        private int gameHeight = 800;
        private Panel mainGamePanel;
        private Panel topGamePanel;
        private Panel bottomGamePanel;
        private FilesManager fileManager;
        /// <summary>
        /// Konstruktor
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            Configurations();
            InitPanels();
            InitGame();
            InitGameMenu();         
            CreateScoreList(scoreListName);
        }
        private void InitGameMenu()
        {
            menu=new GameMenu();
            menu.Left = Width/2-menu.Width/4;
            menu.Top = 100;
            Controls.Add(menu);

            game.RefreshBoard += bottomGamePanel.Refresh;
            
            menu.Start();
            playerName = menu.PlayerName;
            LoadMenuEvents();
        }
        private void Configurations()
        {
               fileManager=new FilesManager();
            Width = gameWidth;
            Height = gameHeight;
               BackColor = Color.Black;

        }
        private void InitPanels()
        {
            mainGamePanel=new Panel();
            topGamePanel = new Panel();
            bottomGamePanel = new Panel();

            mainGamePanel.Dock = DockStyle.Fill;
          

            topGamePanel.Dock = DockStyle.Top;
            bottomGamePanel.Dock = DockStyle.Bottom;

            topGamePanel.Height = (int) (gameHeight*0.9);
            bottomGamePanel.Height = (int) (gameHeight*0.1);

            

            bottomGamePanel.Paint+=new PaintEventHandler(bottomGamePanel_Paint);

            bottomGamePanel.BackColor = Color.Black;

            mainGamePanel.Controls.Add(bottomGamePanel);
            mainGamePanel.Controls.Add(topGamePanel);
            Controls.Add(mainGamePanel);

         
        }
        private void InitGame()
        {
            game=new Game.Game();
            game.SavePointEvent += SavePoints;
           topGamePanel.Controls.Add(game);
        //    game.Start();
            game.EscapeButtonEvent += EscapeKeyPress;
            mainGamePanel.Hide();
        }
        private void CreateScoreList(string name)
        {
            pathScoreList = Directory.GetCurrentDirectory() + @"/" + name;
            if(!File.Exists(pathScoreList))
            {
                List<Score> list=new List<Score>();
                fileManager.UpdateHighScore(list,pathScoreList);
            }
        }
        #region Events
        
        private void LoadMenuEvents()
        {
            ((ButtonMenu)menu.ControlDictionary[NameButton.Easy]).Label.MouseClick += new MouseEventHandler(EasyButtonClick);
            ((ButtonMenu)menu.ControlDictionary[NameButton.Medium]).Label.MouseClick += new MouseEventHandler(MediumButtonClick);
            ((ButtonMenu)menu.ControlDictionary[NameButton.Hard]).Label.MouseClick += new MouseEventHandler(HardButtonClick);
           ((ButtonMenu)menu.ControlDictionary[NameButton.SoundOnOff]).Label.MouseClick += new MouseEventHandler(SoundOnOffButtonClick);
           ((ButtonMenu)menu.ControlDictionary[NameButton.Exit]).Label.MouseClick += new MouseEventHandler(ExitClick);
           
            ((ButtonMenu)menu.ControlDictionary[NameButton.Highscores]).Label.MouseClick += new MouseEventHandler(HighScoresClick);
            
        }
        private void EasyButtonClick(object sender,EventArgs e)
        {
            game.MapLvl = 1;
            game.LevelDifficulty = LevelDifficulty.Padawan;
            menu.Enabled = false;
            menu.Hide();
            game.Start();
            mainGamePanel.Enabled = true;
            mainGamePanel.Show();
        }

        private void MediumButtonClick(object sender, EventArgs e)
        {
            game.MapLvl = 1;
            game.LevelDifficulty = LevelDifficulty.Jedi;
            menu.Enabled = false;
            menu.Hide();

            game.Start();
            mainGamePanel.Enabled = true;
            mainGamePanel.Show();
        }

        private void HardButtonClick(object sender, EventArgs e)
        {
            game.MapLvl = 1;
            game.LevelDifficulty = LevelDifficulty.JediMaster;
            menu.Enabled = false;
            menu.Hide();
            game.Start();
            mainGamePanel.Enabled = true;
            mainGamePanel.Show();
        }
        private void HighScoresClick(object sender,EventArgs e)
        {
          ((ListBox)  menu.ControlDictionary[NameButton.HighscoresListBox]).Items.Clear();
            List<Score> list = fileManager.ReadHighScore(pathScoreList);
            for (int i = 0; i < list.Count;i++ )
            {
                Score score = list[list.Count - 1 - i];
                ((ListBox)menu.ControlDictionary[NameButton.HighscoresListBox]).Items.Add(score.Name + " " + score.Points.ToString());

            }

        }
        private void ExitClick(object sender,EventArgs e)
        {
            Close();
        }
        private void SoundOnOffButtonClick(object sender, EventArgs e)
        {
            Label label = sender as Label;
            string text = label.Text;
            if (text.EndsWith("on"))
            {
                label.Text = (text.Remove(text.Length - 2) + "off");
            }
            else
            {
                label.Text = (text.Remove(text.Length - 3) + "on");
            }
            game.MuteSound = !game.MuteSound;
        }
        private void bottomGamePanel_Paint(object sender,PaintEventArgs e)
        {
            Font f=new Font("Arial",10);
            int separator = 200;
            if (game != null&&game.Started)
            {
                int height =(int) e.Graphics.MeasureString(game.ActualWeaponName, f).Height;
                int width = Math.Max((int) e.Graphics.MeasureString(game.ActualWeaponName, f).Width,
                                     (int) e.Graphics.MeasureString(game.WeaponsName, f).Width);
                e.Graphics.DrawString(game.ActualWeaponName,f,Brushes.GreenYellow,0,0);
                e.Graphics.DrawString(game.WeaponsName, f, Brushes.Gray, 0, height);
                width += separator;
                e.Graphics.DrawImage(game.HeartImage,width,0);
                e.Graphics.DrawString(game.Life.ToString(),f,Brushes.DarkRed,width,game.HeartImage.Height);
                width += Math.Max((int)e.Graphics.MeasureString(game.Life.ToString(), f).Width, game.HeartImage.Width);
                width += separator;
                e.Graphics.DrawImage(game.ShieldImage,width,0);
                e.Graphics.DrawString(game.Armor.ToString(),f,Brushes.LightBlue,width,game.ShieldImage.Height);
                width += Math.Max((int)e.Graphics.MeasureString(game.Armor.ToString(), f).Width, game.ShieldImage.Width);
                width += separator;
                e.Graphics.DrawImage(game.StarImage, width, 0);
                e.Graphics.DrawString(game.Pts.ToString(), f, Brushes.LawnGreen, width, game.StarImage.Height);
            }
        }
        private void EscapeKeyPress()
        {
            if (game.Started)
            {
                if (escapePress)
                {
                    mainGamePanel.Hide();
                    mainGamePanel.Enabled = false;
                    menu.Show();
                    menu.Enabled = true;
                    menu.Start();
                    game.Stop();
                }
                else
                {
                    menu.Enabled = false;
                    menu.Hide();
                    mainGamePanel.Show();
                    mainGamePanel.Enabled = true;
                
                    game.Resume();
                }
                escapePress = !escapePress;
            }
        }
        private void SavePoints(int points)
        {
            playerName = menu.PlayerName;
            List<Score> list = fileManager.ReadHighScore(pathScoreList);
            if(list.Count>=10)
            {
                if(points>list.Min().Points)
                {
                    list.Remove(list.Min());
                    list.Add(new Score(playerName,points));
                    
                }
                else
                {
                    return;
                }
            }
            else
            {
                list.Add(new Score(playerName, points));
                    
            }
            list.Sort();
            fileManager.UpdateHighScore(list,pathScoreList);
        }
        /// <summary>
        /// Obsługa klawiatury
        /// </summary>
        /// <param name="e">Argumetn</param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Escape)
                EscapeKeyPress();
            base.OnKeyDown(e);
        }
        #endregion
    }
}
